package com.ac.controller;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ac.exception.ExceptionService;
import com.ac.model.LogSystem;
import com.ac.model.LogSystemType;
import com.ac.util.IPUtils;

@Controller
public class CErrorController implements ErrorController {

	@RequestMapping("/error")
	@ResponseBody
	public void handleError(HttpServletRequest request, HttpServletResponse response) {
		String requestUri = (String) request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI);
		String ipAddr = IPUtils.getIpAddr(request);
		
		LogSystem logSystem = new LogSystem();
		logSystem.setTitle("路径异常");
		logSystem.setType(LogSystemType.Path);
		logSystem.setContent(requestUri);
		logSystem.setIp(ipAddr);
		logSystem.setCode(com.ac.response.HttpStatus.NOT_FOUND);
		ExceptionService.addLogSystem(logSystem);
		
		response.setStatus(HttpStatus.NOT_FOUND.value());
	}

}
